﻿using GlennQuigmire.API.Application.Commands;
using GlennQuigmire.API.Application.GDtos;
using IMES.Common.Domain.SirModel;
using IMES.Common.Dtos;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace GlennQuigmire.API.Controllers
{
    /// <summary>
    /// 入库单控制器--API
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class InboundOrderController : ControllerBase
    {

        private readonly IMediator mediator;

        public InboundOrderController(IMediator mediator)
        {
            this.mediator = mediator;
        }

        /// <summary>
        /// 添加入库单信息
        /// </summary>
        /// <param name="cmd">添加入库单请求对象</param>
        /// <returns>添加入库单请求对象响应对象</returns>
        [HttpPost]
        public Task<Result> InboundOrderCreate(InboundOrderCreateCommand cmd)
        {
            return mediator.Send(cmd);
        }


        /// <summary>
        /// 根据id查询入库单的产品信息
        /// </summary>
        /// <param name="cmd">根据id查询入库单的产品信息请求对象</param>
        /// <returns>根据id查询入库单的产品信息响应对象</returns>
        [HttpPost]
        public Task<ResultT<Product>> GetInboundOrderProductDetail(InboundOrderProductDetailCommand cmd)
        {
            return mediator.Send(cmd);
        }


        /// <summary>
        /// 查询入库单（入库单显示）
        /// </summary>
        /// <param name="cmd">查询入库单请求对象</param>
        /// <returns>查询入库单响应对象</returns>
        [HttpPost]
        public Task<ResultT<InboundOrderListDto>> InboundOrderSearch(InboundOrderSearchCommand cmd)
        {
            return mediator.Send(cmd);
        }

        /// <summary>
        /// 查询入库类型
        /// </summary>
        /// <param name="request">查询入库类型请求对象</param>
        /// <returns>返回入库类型响应对象</returns>
        [HttpGet]
        public async Task<ResultT<InboundOrderType>> InboundOrderTypeSearch([FromQuery]InboundOrderTypeSearchCommand cmd)
        {
            return await mediator.Send(cmd);
        }


        /// <summary>
        /// 根据选择的入库单显示
        /// </summary>
        /// <param name="cmd">选择的入库单主键</param>
        /// <returns>返回入库单商品列表</returns>
        [HttpGet]
        public Task<ResultT<InboundOrderProductsDto>> GetInboundOrderProducts([FromQuery]InboundOrderProductsSearchCommand cmd)
        {
            return mediator.Send(cmd);
        }



        /// <summary>
        /// 删除入库单及其关联的入库单商品
        /// </summary>
        /// <param name="cmd">要删除的入库单Id</param>
        /// <returns>返回删除响应对象</returns>
        [HttpDelete]
        public Task<Result> InboundOrderDelete(InboundOrderAllDeleteCommand cmd)
        {
            return mediator.Send(cmd);
        }


        /// <summary>
        /// 反添入库单信息
        /// </summary>
        /// <param name="cmd">要反添的入库单主键</param>
        /// <returns>返回入库单信息</returns>
        [HttpPost]
        public Task<ResultT<InboundOrderDetailDto>> InoutboundOrderDetail(InboundOrderDetailCommand cmd)
        {
            return mediator.Send(cmd);
        }


        /// <summary>
        /// 修改入库单
        /// </summary>
        /// <param name="cmd">修改的入库单信息</param>
        /// <returns>返回影响行数</returns>
        [HttpPost]
        public Task<Result> InboundOrderUpdate(InboundOrderUpdateCommand cmd)
        {
            return mediator.Send(cmd);
        }

        /// <summary>
        /// 导出入库单excel文件
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpGet]
        public async  Task<IActionResult> ExportInboundOrderToExcel([FromQuery]ExcelLeadIntoCommand cmd)
        {
            var result = await mediator.Send(cmd);
            return File(result.FileBytes, result.MimeType, result.FileName);
        }

        /// <summary>
        /// 导入入库单
        /// </summary>
        /// <param name="cmd">要导入的excel文件</param>
        /// <returns>返回导入结果提示</returns>
        [HttpPost]
        public async Task<Result> ExcelLeadIn(ExcelLeadOutCommand cmd)
        {
            return await mediator.Send(cmd);
        }

        /// <summary>
        /// 查询入库单（打印）
        /// </summary>
        /// <param name="cmd">打印入库单请求对象</param>
        /// <returns>返回打印的入库单信息</returns>
        [HttpPost]
        public Task<Result> PrintInboundOrder(InboundOrderPrintCommand cmd)
        {
            return mediator.Send(cmd);
        }


    }
}
